import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏西西
 * Date: 2023-11-05
 * Time: 20:18
 */
public class MySingleList {

    static class ListNode {
        public int val;//存储的数据
        public ListNode next;//存储下一个节点的地址；

        //提供一个构造方法
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;//代表当前链表头结点的引用

    public void createLink(){
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(1);
        ListNode listNode3 = new ListNode(1);
        ListNode listNode4 = new ListNode(1);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        head = listNode1;
    }
    /**
     * 遍历链表
      */
    public void display(){
        //如果要将整个链表遍历完成 那么就要head == null;
        //如果是遍历到链表的尾巴。条件是head.next == null
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 从指定位置开始打印
     */
    public void display(ListNode newHead){
        //如果要将整个链表遍历完成 那么就要head == null;
        //如果是遍历到链表的尾巴。条件是head.next == null
        ListNode cur = newHead;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //查找关键字key是否在单链表中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到链表的长度
    public int size(){
       int count = 0;
       ListNode cur = head;
       while(cur != null){
           count++;
           cur = cur.next;
       }
       return count;
    }
    //头插法
    public void addFirst(int data){
        ListNode listNode = new ListNode(data);
        listNode.next = head;
        head = listNode;
    }
    //尾插法
    public void addLast(int data){
        ListNode listNode = new ListNode(data);
        //判断是不是第一次插入
        if(head == null){
            head = listNode;
            return;
        }
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = listNode;
    }
    //任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index,int data)
            throws ListIndexOutOfException{
        checkIndex(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode cur = findIndexSubOne(index);
        ListNode listNode = new ListNode(data);
        listNode.next = cur.next;
        cur.next = listNode;
    }

    /**
     * 找到index-1位置上的节点的地址
     * @param index
     * @return
     */
    private ListNode findIndexSubOne(int index){
        ListNode cur = head;
        int count = 0;
        while (count != index -1){
            cur = cur.next;
            count++;
        }
        return cur;
    }
    private void checkIndex(int index){
        if(index < 0 || index > size()){
            throw new ListIndexOutOfException("index位置不合法！");
        }
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = searchPrev(key);//代表当前节点
        if(cur == null){
            return ;
        }
        ListNode del = cur.next;//代表要删除的节点
        cur.next = del.next;
    }

    /**
     * 找到关键字key的前一个节点
     * @param key
     * @return
     */
    private ListNode searchPrev(int key){
        if(head == null){
            return null;//一个节点都没有
        }
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        //代表根本没有要删除的节点
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key){
            head = head.next;
        }
    }

    /**
     * 清空所有的节点
     */
    public void clear(){
        head = null;
    }

    /**
     * 反转链表
     * @return
     */
    public ListNode reverseList(){
        if(head == null){
            return null;
        }
        //走到这里就说明链表里只有一个节点
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null){
            ListNode curNext = cur.next;
            //头插法插入cur（任何的插入都是先绑后面）
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    public ListNode middleNode(){
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    //返回倒数第k个节点
    public ListNode findKthToTail(int k){
        //当k不合法的时候
        if(k <= 0 || head == null){
            return null;
        }

        //当k合法时
        ListNode fast = head;
        ListNode slow = head;
        //1.fast走k-1步
        while (k - 1 != 0){
            fast = fast.next;
            if(fast == null){
                return null;
            }
            k--;
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    public boolean chkPalindrome(){
        if(head == null){
            return  false;
        }
        if(head.next == null){
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
        //1.找中间节点
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.翻转
        ListNode cur = slow.next;//cur代表当前需要翻转的节点
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //3.一个从头往后，一个从后往前
        while (slow != head){
            if(head.val != slow.val){
                return false;
            }
            //偶数情况的判断
            if(head.next == slow){
                return true;
            }
            slow = slow.next;
            head = head.next;
        }
        return true;
    }

    public ListNode partition(int x){
        ListNode as = null;
        ListNode ae = null;
        ListNode bs = null;
        ListNode be = null;

        ListNode cur = head;
        while(cur != null){
            if(cur.val < x){
                if(bs == null){
                    bs = cur;
                    be = cur;
                }else{
                    be.next = cur;
                    be = be.next;

                }
            }else {
                if(as == null){
                    as = cur;
                    ae = cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        //有可能不会同时存在小于x 和大于等于x的数据
        if(bs == null){
            return as;
        }
        //第一段不为空
        be.next = as;
        //第2段不为空的问题
        if(as != null){
            ae.next = null;
        }
        return bs;
    }
    public boolean hasCycle(){
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
               break;
            }
        }
        if(fast == null || fast.next == null){
            return false;
        }
        return true;
    }
    public void createLoop(){
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = head.next.next;
    }
    public ListNode detectCycle(){
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return null;
        }
        //有环，此时fast和slow就相遇了
        slow = head;
        while(fast != slow){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
